package org.adl.registry.website;

import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Jacob Marks - Joint ADL Co-Lab
 * @version 1.0
 * @license This software is licensed as open source software pursuant to the terms defined in the Joint ADL Co-Lab Open Source Software License.  Use of any part of this software constitutes acceptance of this license. The Joint ADL Co-Lab open source agreement is available at {@link http://adlregistry.adlnet.gov/source_license.html}.
 */
public abstract class RequestHandler
{
    private HttpServletRequest mRequest;
    private ServletContext mContext;
    private String mRegistry;
    private String mRimUrl;
    private String mClientAccessKey;

    public RequestHandler()
    {
        
    }
    
    /**
     * Gets the <CODE>ServletRequest</CODE> provided to the handler at instantiation.
     * @return Returns the <CODE>ServletRequest</CODE> which was provided to the handler at instantiation.
     */
    public HttpServletRequest getServletRequest()
    {
        return this.mRequest;
    }
    
    protected void setServletRequest(HttpServletRequest iRequest)
    throws InvalidRequestException
    {
        if (iRequest == null)
        {
            throw new InvalidRequestException("A servlet request is required in order to process a request.");
        }
        
        this.mRequest = iRequest;
    }
    
    /**
     * Gets the <CODE>ServletRequest</CODE> provided to the handler at instantiation.
     * @return Returns the <CODE>ServletRequest</CODE> which was provided to the handler at instantiation.
     */
    public ServletContext getServletContext()
    {
        return this.mContext;
    }
    
    protected void setServletContext(ServletContext iContext)
    throws InvalidRequestException
    {
        if (iContext == null)
        {
            throw new InvalidRequestException("A servlet context is required in order to process a request.");
        }
        
        this.mContext = iContext;
    }
    
    public abstract String InvokeRequest() throws IOException, InvalidRequestException;

    public String getRimUrl()
    {
        return this.mRimUrl;
    }

    protected void setRimUrl(String iRimUrl)
    throws InvalidRequestException
    {
        if ((iRimUrl == null) || (iRimUrl.trim().length() == 0))
        {
            throw new InvalidRequestException("The location of the ADL Registry Interface Mechanism is required in order to make a request to the ADL Registry.");
        }
        
        this.mRimUrl = iRimUrl;
    }
    
    /**
     * Ges the <CODE>String</CODE> registry handle of the registry which has been set as the target of the request.
     * @return Returns the <CODE>String</CODE> registry handle of the registry which has been set as the target of the request.
     */
    public String getRegistry()
    {
        return this.mRegistry;
    }
    
    /**
     * 
     * @param registry 
     * @throws java.lang.NullPointerException 
     */
    protected void setRegistry(String iRegistryHandle)
    throws InvalidRequestException
    {
        if ((iRegistryHandle == null) || (iRegistryHandle.trim().length() == 0))
        {
            throw new InvalidRequestException("The target registry must be provided in order to process a basic search request.");
        }
        
        iRegistryHandle = iRegistryHandle.trim();
        
        this.mRegistry = iRegistryHandle;
    }
    
    public String getClientAccessKey()
    {
        return this.mClientAccessKey;
    }

    protected void setClientAccessKey(String iClientAccessKey)
    throws InvalidRequestException
    {
        if ((iClientAccessKey == null) || (iClientAccessKey.trim().length() == 0))
        {
            throw new InvalidRequestException("A client access key is required in order to make a request to the ADL Registry.");
        }
        
        this.mClientAccessKey = iClientAccessKey;
    }
}
